<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Actions\Form;

use App\Models\Withdrawal;
use App\Service\User\BeanAccountService;
use App\Service\User\RewardAccountService;
use Dcat\Admin\Admin;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class WithdrawalRejectForm extends Form implements LazyRenderable
{
    use LazyWidget;

    /*
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    use LazyWidget;

    public function handle(array $input)
    {
        $id = $input['id'];
        $message = $input['message'];

        $model = Withdrawal::query()->findOrFail($id);
        $model->status = 3;
        $model->audit_admin = Admin::user()->name;
        $model->audited_at = now()->toDateTimeString();
        $model->processed_at = now()->toDateTimeString();
        $model->reject_reason = $message;
        $model->save();

        RewardAccountService::make()->recharge(
            $model->user_id,
            $model->amount,
            5,
            sprintf('提现到银行卡被驳回！退回金额:%s 驳回原因:%s', $model->amount, $message),
        );
        if ($model->bean > 0) {
            BeanAccountService::make()->recharge(
                $model->user_id,
                $model->bean,
                sprintf('提现到银行卡被驳回！退回KC通证:%s 驳回原因:%s', $model->bean, $message),
                false,
                3
            );
        }

        return $this->response()
            ->success('已驳回（原因：' . $message . '）')
            ->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->text('message', '驳回原因')->required();
        //批量选择的行的值怎么传递看下面
        $this->hidden('id')->default($this->payload['id']);
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        //设置默认值
        return [
        ];
    }
}
